package com.mysoft.gfyx.clue.controller.option;

import com.mysoft.framework.modeling.OptionDataController;
import com.mysoft.framework.modeling.dto.Filter;
import com.mysoft.framework.modeling.dto.OptionItem;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.clue.controller.dto.Perm.GetUserAreaPermissionResponseDto;
import com.mysoft.gfyx.clue.service.DataPermissionService;
import com.mysoft.gfyx.clue.service.ProfitProvinceService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Tag(name = "地区备选项-只有省份-API")
@PubService(value = "/areaProvinceOptionData", prefix = RequestPrefix.API, method = {})
public class AreaProvinceOptionDataController extends OptionDataController {

    @Autowired
    private ProfitProvinceService profitProvinceService;

    @Autowired
    private DataPermissionService dataPermissionService;

    @Override
    protected List<OptionItem> load(List<Filter> filters, Map<String, String> urlParams) {
        List<OptionItem> profitProvinceList = profitProvinceService.getProfitProvinceList();
        // 用户拥有的权限地区（只包含省）
        GetUserAreaPermissionResponseDto userAreaPermissionDto = dataPermissionService.getUserPermissionArea();

        //如果用户拥有全国权限或者是没有设置地区权限,就取权益地区返回
        if (userAreaPermissionDto == null
                || userAreaPermissionDto.getArea() == null
                || userAreaPermissionDto.getArea().contains("0")
                || userAreaPermissionDto.getArea().size() == 0) {
            return profitProvinceList;
        }

        List<OptionItem> result = new ArrayList<>();
        profitProvinceList.forEach(p -> {
            if (userAreaPermissionDto.getArea().contains(p.getValue())) {
                //取权益和权限的交集
                result.add(p);
            }
        });

        return result;
    }
}
